package edu.chinasoft.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

import edu.chinasoft.util.PropertiesTool;

/**
 * Application Lifecycle Listener implementation class OnLineForListener02
 *
 */
@WebListener
public class OnLineForListener02 implements HttpSessionAttributeListener,ServletContextListener {

    /**
     * Default constructor. 
     */
    public OnLineForListener02() {
        // TODO Auto-generated constructor stub
    }

	/**
     * @see HttpSessionAttributeListener#attributeAdded(HttpSessionBindingEvent)
     */
    //触发添加元素时执行
    public void attributeAdded(HttpSessionBindingEvent arg0)  { 
         // TODO Auto-generated method stub
    	
       	//获取添加到session容器里的元素的key
    	String key = arg0.getName();
    	if(key.equals("user")) {        //登录成功
       	    //获取serveltcontext里数据
        	HttpSession session = arg0.getSession();
        	ServletContext servletContext = session.getServletContext();
        	Integer number = (Integer)servletContext.getAttribute("online");
        	
        	Integer visitnumber = (Integer)servletContext.getAttribute("visitnumber");
        	
        	
        	
        	//数据+1,  重新保存到servletcontext对象里
        	if(number==null) {
        		servletContext.setAttribute("online", 1);
        	}else {
        		servletContext.setAttribute("online", number+1);
        		
        	}	
        	
        	
        	servletContext.setAttribute("visitnumber", visitnumber+1);
    	}
    }

	/**
     * @see HttpSessionAttributeListener#attributeRemoved(HttpSessionBindingEvent)
     */
    //触发移除元素时执行
    public void attributeRemoved(HttpSessionBindingEvent arg0)  { 
         // TODO Auto-generated method stub
       	//获取添加到session容器里的元素的key
    	String key = arg0.getName();
    	if(key.equals("user")) {     //表示正常退出
    	   	 //获取serveltcontext里数据
    	   	HttpSession session = arg0.getSession();
    	   	ServletContext servletContext = session.getServletContext();
    	   	Integer number = (Integer)servletContext.getAttribute("online");
    	   	
    	   	//数据-1,  重新保存到servletcontext对象里

    	   		servletContext.setAttribute("online", number-1);
    	}
    	
 
    	
    	
    	
    	
    }

	/**
     * @see HttpSessionAttributeListener#attributeReplaced(HttpSessionBindingEvent)
     */
    //触发替换元素时执行
    public void attributeReplaced(HttpSessionBindingEvent arg0)  { 
         // TODO Auto-generated method stub

    	
    	
    	
    }
    //服务器关闭的时候
	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		// TODO Auto-generated method stub
		//获取servletcontext里的值
		ServletContext servletContext = arg0.getServletContext();
		int visitNumber = (Integer)servletContext.getAttribute("visitnumber");	
		
		//然后把值写入到容器里
		String path = servletContext.getRealPath("/config/visit.properties");
		PropertiesTool.writerProperties(visitNumber, path);
	}
    //服务器启动的时候
	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		// TODO Auto-generated method stub
		//读取属性文件里的值
		ServletContext servletContext = arg0.getServletContext();
		String path = servletContext.getRealPath("/config/visit.properties");
		int number = PropertiesTool.readProperties(path);
		
		//把读取到的值保存到serveltcontext里
		servletContext.setAttribute("visitnumber", number);
		
	}
	
}
